<?php
class Comment_Model extends Model {
	var $_table = "comments";
	var $_dateFormat = "%Y-%m-%d %H:%i:%s";
	
	function Comment_Model(){
		parent::Model();
	}
	
	function get($params = array(), $start = -1, $offset = -1){
		$DB = $this->_get($params);
		
		$DB->select ( $this->_table . ".*" );
		$DB->select ( "nodes.id as node_id, nodes.type as node_type, nodes.title as node_title, nodes.url_name as nodes_url_name" );
		$DB->select ( "users.id as user_id, users.email as user_email, users_meta.first_name as user_first_name, users_meta.last_name as user_last_name" );
		if ($start != - 1 && $offset != - 1) {
			$DB->limit ( $offset, $start );
		}
		
		$query = $DB->get ();
		
		if((isset($params['id']) && $params['id'] != NULL && $params['id'] !=-1)){
				
			if ($query->num_rows() > 0){
				return $query->row();
			}
			
			return NULL;
				
		}
		
		return $query->result ();
	}
	
	function getCount($params = array()){
		$DB = $this->_get($params);

		return $DB->count_all_results ();
	}
	
	function _getParams($params = array()){
		$defaults = array(
			// No tiene sentido utilizar estos
			"id"			=> NULL,
			
			"node_id" 		=> NULL,
			"user"			=> NULL,
			"ip"			=> NULL,
			"status"		=> "approved", //approved, denied, spam, hidden, pending, all
			"node_type"		=> NULL,
			"categories"	=> array(),
			"categories_or"	=> array(),
			"tags"			=> array(),

			"order"		=> array(
				"comment_created"	=> "desc"
			)
			
		);
		
		$params = array_merge($defaults, $params);
		
		
		if($params['node_type'] != NULL && is_string($params['type'])){
			$params['node_type'] = split(",", $params['node_type']);
		}
		if($params['categories'] != NULL && is_string($params['categories'])){
			$params['categories'] = split(",", $params['categories']);
		}
		if($params['categories_or'] != NULL && is_string($params['categories_or'])){
			$params['categories_or'] = split(",", $params['categories_or']);
		}
		
		return $params;
	}
	
	function _get($params = array()){
		$params = $this->_getParams($params);
		
		$this->db->from ( $this->_table );
		
		//Hacemos join con los nodos.
		$this->db->join ( "nodes", "nodes.id = comments.node_id" );
		
		$this->db->join ( "users", "comments.user_id = users.id", "left");
		$this->db->join ( "users_meta", "users.id = users_meta.user_id", "left");
		
		if($params['status'] != NULL && $params['status'] != "all" ){
			$this->db->where("$this->_table.comment_status = '{$params['status']}'");
			/*
			if($params['status'] == "approved"){
				$this->db->where("$this->_table.comment_status = 'approved'");
			} else if($params['status'] == "denied"){
				$this->db->where("$this->_table.comment_status = 'no_approbed'");
			} else if($params['status'] == "span"){
				$this->db->where("$this->_table.comment_status = 'span'");
			} else if($params['status'] == "hidden"){
				$this->db->where("$this->_table.comment_status = 'hidden'");
			}*/
		}
		
		if($params['id'] != NULL && $params['id'] != -1){
			$this->db->where ( "$this->_table.comment_id", $params['id'] );
		} else{
			
			//@TODO resto de filtros
			if($params['node_id'] != NULL){
				$this->db->where("nodes.id", $params['node_id']);
			}
			
			if($params['ip'] != NULL){
				$this->db->where("comments.comment_ip", $params['ip']);
			}
			
			if($params['user'] != NULL){
				if(is_numeric($params['user'])){
					$this->db->where("comments.user_id", $params['user']);
				} else{
					$this->db->where("comments.comment_name", $params['user']);
				}
			}
			
			
			//Si hacemos filtro de nodos y demas, tendremos que filtrar por status de estos
			
			if($params['node_type'] != NULL && is_array($params['node_type']) && count($params['node_type'])>0){
				
				$where_query = "(";
				$num_params = count($params['node_type']);
				foreach($params['node_type'] as $pos => $type){
					$where_query .= "nodes.type = '$type'";
					if($pos<($num_params - 1)){
						$where_query .= " OR ";
					}
				}
				$where_query .= ")";
				
				$this->db->where($where_query);
			}
			
			//Si hacemos filtro de categorias, tendremos que tener en cuenta su estado
			
			if($params['categories'] != NULL && is_array($params['categories']) && count($params['categories'])>0){
				foreach($params['categories'] as $pos=>$category){
					$this->db->join ( "nodes_categories as nc_$pos", "nc_$pos.id_node = nodes.id" );
					
					if(is_numeric($category)){
						$this->db->where("nc_$pos.id_category", $category);
					} else{
						$this->db->join ( "categories as c_$pos", "c_$pos.id = " . "nc_$pos.id_category" );
						$this->db->where ( "c_$pos.name", $category );
					}
				}
			}
			
			if($params['categories_or'] != NULL && is_array($params['categories_or']) && count($params['categories_or'])>0){
				$this->db->join ( "nodes_categories as nc_or", "nc_or.id_node = nodes.id" );
				$this->db->join ( "categories as c_or", "c_or.id = " . "nc_or.id_category" );
				
				$where_query = "(";
				$num_params = count($params['categories_or']);
				foreach($params['categories_or'] as $pos=>$category){
					if(is_numeric($category)){
						$where_query .= "c_or.id = $category";
					} else{
						$where_query .= "c_or.url_name = '$category'";
					}
					if($pos<($num_params - 1)){
						$where_query .= " OR ";
					}
				}
				$where_query .= ")";
				
				$this->db->where($where_query);
			}
			
			if($params['tags'] != NULL && is_array($params['tags']) && count($params['tags'])>0){
				foreach($params['tags'] as $pos=>$tag){
					$this->db->join ( "nodes_tags as nt", "nt.node_id = nodes.id" );
					$this->db->join ( "tags as t", "t.id = " . "nt.tag_id" );
					
					$where_query = "(";
					$num_params = count($params['tags']);
					foreach($params['tags'] as $pos=>$tag){
						
						if(is_numeric($tag)){
							$where_query .= "nt.tag_id = $tag";
						} else{
							$where_query .= "t.name = $tag";
						}
						if($pos<($num_params - 1)){
							$where_query .= " OR ";
						}
					}
					$where_query .= ")";
					
					$this->db->where($where_query);
				}
			}
			
			if($params['order'] != NULL){
				foreach($params['order'] as $field => $direction){
					$this->db->order_by ( "$field $direction" );
				}
			}
		}
		
		return $this->db;
	}
	
		
	
	function create($field_data = array()){
		
		$defaults = array(
			'parent_id'	=> NULL,
			'status'	=> 'pending', //approve, spam, hidden, denied, pending
			'user_id'	=> NULL,
			'name'		=> NULL,
			'email'		=> NULL,
			'url'		=> NULL,
			'ip'		=> $this->input->ip_address(),		
			'created'	=> mdate($this->_dateFormat),
			'modified'	=> NULL,
			'data'		=> ''
		);
		
		$field_data = array_merge($defaults, $field_data);
		
		$data = array (
			'node_id'			=> $field_data['node_id'],
			'comment_parent_id'	=> $field_data['parent_id'],
			'comment_status'	=> $field_data['status'],
			'user_id'			=> $field_data['user_id'],
			'comment_name' 		=> $field_data['name'],
			'comment_email' 	=> $field_data['email'],
			'comment_url'		=> $field_data['url'],
			'comment_ip' 		=> $field_data['ip'],
			'comment_created' 	=> $field_data['created'],
			'comment_modified'	=> $field_data['modified'],
			'comment_data' 		=> $field_data['data']
		);

		$this->db->insert($this->_table, $data);
		
		$id = $this->db->insert_id();
		
		return $id;
	}


	function update($field_data = array()){
		$comment = $this->get(array("id" => $field_data['id'], "status" => "all"));
		
		
		if($comment != NULL){
			$defaults = array(
				'id'		=> $comment->comment_id,
				'node_id'	=> $comment->node_id,
				'parent_id'	=> $comment->comment_parent_id,
				'status'	=> $comment->comment_status,
				'user_id'	=> $comment->user_id,
				'name'		=> $comment->comment_name,
				'email'		=> $comment->comment_email,
				'url'		=> $comment->comment_url,
				'ip'		=> $comment->comment_ip,		
				'modified'	=> mdate($this->_dateFormat),
				'data'		=> $comment->comment_data
			);
			
			$field_data = array_merge($defaults, $field_data);
			
			$data = array (
				'comment_id'		=> $field_data['id'],
				'node_id'			=> $field_data['node_id'],
				'comment_parent_id'	=> $field_data['parent_id'],
				'comment_status'	=> $field_data['status'],
				'user_id'			=> $field_data['user_id'],
				'comment_name' 		=> $field_data['name'],
				'comment_email' 	=> $field_data['email'],
				'comment_url'		=> $field_data['url'],
				'comment_ip' 		=> $field_data['ip'],
				'comment_modified'	=> $field_data['modified'],
				'comment_data' 		=> $field_data['data']
			);
			
			$this->db->where('comment_id', $field_data['id']);
			$this->db->update($this->_table, $data);
			
			return TRUE;
		}
		
		return FALSE;
	}
	
	function delete($id){		
		$this->db->where('comment_id', $id);			
		$this->db->delete($this->_table);
	}
	
	function update_comment_count($node_id){
		//Actualizariamos el valor de numero de comentarios en nodes
		$comments_param = array("node_id" => $node_id, "status" => "approved");
		$comments_count = $this->getCount($comments_param);
		
		$data = array("comment_count" => $comments_count);
		$this->db->where('id', $node_id);
		$this->db->update("nodes", $data);
	}
}
?>
